﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using Biz_Dal.Biz;

public partial class The_hien_TH_DANH_SACH_SACH : System.Web.UI.UserControl
{

    List<BizSach> Saches;
    int soSach;

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
            ViewState["So_sach"] = "6";

            if (Request.QueryString["TimKiem"] == "1")//tìm kiếm đơn giản
            {
                Saches = BizSach.getListLikeSachByName(Request.QueryString["keyword"].ToString());//theo tên
            }
            else if (Request.QueryString["TimKiem"] == "2")//tìm kiếm nâng cao
            {
                string tenS = Request.QueryString["Ten_sach"].ToString();
                string idTL = Request.QueryString["IDTL"].ToString();
                string idNXB = Request.QueryString["IDNXB"].ToString();
                string giaBD = Request.QueryString["GiaBD"].ToString();
                string giaKT = Request.QueryString["GiaKT"].ToString();
                Saches = BizSach.getListSachByMulti(tenS, int.Parse(idTL), int.Parse(idNXB), double.Parse(giaBD), double.Parse(giaKT));
                //Saches = BizSach.getListSachAll();
            }
            else//không tìm kiếm
            {
                if (string.IsNullOrEmpty(Request.QueryString["Mcd"]))//sách mới
                {
                    Saches = BizSach.getListSachAll();
                }
                else
                {
                    Saches = BizSach.getListSachByidTheLoai(Convert.ToInt32(Request.QueryString["Mcd"] as string));//sách theo chủ đề
                }
            }


        if (Saches !=null)
        {
            soSach = Convert.ToInt32(ViewState["So_sach"].ToString());
            CollectionPager1.MaxPages = 10000;
            CollectionPager1.PageSize = int.Parse(ViewState["So_sach"].ToString());
            CollectionPager1.DataSource = Saches;
            Load_sach();
            PanelTonTai.Visible = true;
            PanelKhongTonTai.Visible = false;
        }
        else
        {
            PanelTonTai.Visible = false;
            PanelKhongTonTai.Visible = true;
        }
        
    }

    private void Load_sach()
    {
        CollectionPager1.BindToControl = DataListSach;
        DataListSach.DataSource = CollectionPager1.DataSourcePaged;
        DataListSach.DataKeyField = "idSach";
        DataListSach.DataBind();
    }


    protected void DropDownListSoSach_SelectedIndexChanged(object sender, EventArgs e)
    {
        ViewState["So_sach"] = DropDownListSoSach.SelectedValue.ToString();
        if (Saches != null)
        {
            soSach = Convert.ToInt32(ViewState["So_sach"].ToString());
            CollectionPager1.MaxPages = 10000;
            CollectionPager1.PageSize = soSach;
            CollectionPager1.DataSource = Saches;
            Load_sach();
            PanelTonTai.Visible = true;
            PanelKhongTonTai.Visible = false;
        }
        else
        {
            PanelTonTai.Visible = false;
            PanelKhongTonTai.Visible = true;
        }
    }

    #region "Giỏ hàng"
    //int kiem_tra_ton_tai(string ms)
    //{
    //    ArrayList gio_hang = (ArrayList)Session["gio_hang"];
    //    for (int i = 0; i <= gio_hang.Count - 1; i++)
    //        if (((GIO_HANG)gio_hang[i]).Ms == ms)
    //            return i;
    //    return -1;
    //}



    protected void DataListSach_ItemCommand(object source, DataListCommandEventArgs e)
    {
        //if (e.CommandName == "Mua")
        //{
        //    //tính số tiền
        //    Label don_gia = (Label)e.Item.FindControl("don_gia");
        //    double so_tien = double.Parse((Session["So_tien"].ToString()));
        //    so_tien += double.Parse(don_gia.Text);
        //    Session["So_tien"] = so_tien;
        //    //thêm vào giỏ hàng
        //    GIO_HANG giohang = new GIO_HANG();
        //    giohang.Id = int.Parse(DataListSach.DataKeys[e.Item.ItemIndex].ToString());
        //    giohang.Ms = ((Label)e.Item.FindControl("Ma_sach")).Text;
        //    giohang.Ten_sach = ((Label)e.Item.FindControl("Ten_sach")).Text;
        //    giohang.So_luong = 1;
        //    giohang.Don_gia = double.Parse(don_gia.Text);
        //    //kiểm tra và thêm vào giỏ hàng
        //    ArrayList mang = (ArrayList)Session["Gio_hang"];
        //    int ktra = kiem_tra_ton_tai(giohang.Id);
        //    if (ktra < 0)
        //        mang.Add(giohang);
        //    else
        //        ((GIO_HANG)mang[ktra]).So_luong++;
        //    Session["Gio_hang"] = mang;
        //}
    }

    //int kiem_tra_ton_tai(int id)
    //{
    //    ArrayList gio_hang = (ArrayList)Session["gio_hang"];
    //    for (int i = 0; i <= gio_hang.Count - 1; i++)
    //        if (((GIO_HANG)gio_hang[i]).Id == id)
    //            return i;
    //    return -1;
    //}

    #endregion
    protected void DataListSach_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}